from werkzeug.security import generate_password_hash,check_password_hash
from flask import Blueprint,request,jsonify
import json
import re
from conf.comm import sqlalchemy_context
from database import *
#蓝图初始化
bp_users = Blueprint('users',__name__)
#注册功能实现
@bp_users.route('/reg',methods=['POST','GET','PUT','DELETE'])
@sqlalchemy_context()
def reg():
    data = request.data
    if not data:
        data = request.form
    else:
        data = json.loads(request.data)
    mobile = data['mobile']
    if not re.match('^1[3-9]\d{9}$', mobile):
        return jsonify({"code":11111})
    try:
        if request.method == 'POST':
            u = User(username=data['username'],password=generate_password_hash(data['password']),mobile=data['mobile'])
            db.session.add(u)
            db.session.commit()
            return jsonify({"code":200})
    except:
        return jsonify({"code":10010})